package com.sysu.minsysu.zdkb;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.format.Time;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sysu.minsysu.zdkb.utils.Course;
import com.sysu.minsysu.zdkb.utils.CourseUtils;

public class MyAllCourseView extends RelativeLayout{

	private static final String TAG = "MyAllCourseView";
	
	Context mContext;
	
	ArrayList<Course> mCourseList;
	int mWidth;
	int mHeight;
	
	ArrayList<SubCourseView> mSubCourseViews = new ArrayList<SubCourseView>();
	BackgroundView mBackgroundView;
	
	private static final String[] CHINESENUMBERCHAR = new String[]{
		"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六"
	};
	
	Paint mPaint = new Paint();
	
	int mLeftBarWidth;

	int mWeekBarHeight;

	int mRowHeight;

	int mColumnWidth;

	int mWeekCharSize = 10;

	int mLeftCharSize = 10;

	int mCourseInfoCharSize = 9;

	public MyAllCourseView(Context context, ArrayList<Course> courses){
		this(context);
		setLayoutParams( new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT) );
		mContext = context;
		setMyCourses(courses);
		mPaint.setAntiAlias(true);
		setupViews();
	}
	
	public MyAllCourseView(Context context) {
		super(context);
		mContext = context;
		setupViews();
	}
	public MyAllCourseView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		mContext = context;
		setupViews();
	}
	public void setMyCourses( ArrayList<Course> courses ){
		mCourseList = courses;
	}
	
	public void setupViews(){
		mBackgroundView = new BackgroundView(getContext());
		addView(mBackgroundView, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		
		if( mCourseList==null )
			return;
		
		LayoutParams ly = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		
		for( Course course : mCourseList ){
			SubCourseView view = new SubCourseView(getContext(), course);
			//view.setLayoutParams(ly);
			mSubCourseViews.add( view );
			addView(view, new LayoutParams(ly));
		}
	}
	
	@Override
	protected void onLayout( boolean changed, int left, int top, int right, int bottom ){
		mWidth = right - left;
		mHeight = bottom - top;
		super.onLayout(changed, left, top, right, bottom);
			for (SubCourseView view : mSubCourseViews) {
				Course course = view.getCourse();
				int[] pos = coursePosInView(course);
				view.layout(pos[0]+1, pos[1]+1, pos[2]-1, pos[3]-1);
			}
	}
	
	
	private int[] coursePosInView( Course course ){
		int[] pos = new int[4];
		pos[0] = mLeftBarWidth + ( course.getDateOfNumber()-1 ) * mColumnWidth;
		pos[1] = mWeekBarHeight + course.getTimeStart() * mRowHeight;
		pos[2] = pos[0]+ mColumnWidth;
		pos[3] = pos[1] + mRowHeight * course.getClassCount();
		return pos;
	}
	
	private class BackgroundView extends View{

		public BackgroundView(Context context) {
			super(context);
		}

		@Override
		public void onDraw(Canvas canvas){
			Log.d(TAG, mWidth + " " + mHeight);
			//draw background
			canvas.drawColor(Color.RED);
			mPaint.setColor(Color.WHITE);
			canvas.drawRect(mLeftBarWidth, mWeekBarHeight, mWidth, mHeight, mPaint);
			
			//draw titlebar
			drawWeekText(canvas);
			
			//draw leftbar
			drawLeftBar(canvas);
			
			drawLines(canvas);
			
		}

		@Override
		protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec){
			Log.d(TAG, widthMeasureSpec + " " + heightMeasureSpec);
			
			calculateHeight(mWidth, mHeight);
			setMeasuredDimension(mWidth, mHeight);
		}

		private void calculateHeight( int width, int height ){
			mLeftBarWidth = width/6 + 7;
			mWeekBarHeight = height/17;
			mRowHeight = height/17;
			mColumnWidth = ( width-mLeftBarWidth )/6;
			mWeekCharSize = mWeekBarHeight/2;
			mLeftCharSize = mRowHeight/3;
		}

		private void drawLines( Canvas canvas ){
			mPaint.setColor(Color.BLACK);
			mPaint.setStrokeWidth(2);
			// draw horizontal line
			int topSpec = mWeekBarHeight;
			for( int i=0; i<17; i++ ){
				canvas.drawLine(0, topSpec+mRowHeight*i, mWidth, topSpec+mRowHeight*i, mPaint);
			}
			
			// draw vertival line
			int leftSpec = mLeftBarWidth;
			for( int i=0; i<6; i++ ){
				canvas.drawLine(leftSpec+mColumnWidth*i, mWeekBarHeight, leftSpec+mColumnWidth*i, mWeekBarHeight + mRowHeight*17, mPaint);
			}
		}
		

		private void drawLeftBar( Canvas canvas ){
			mPaint.setColor( Color.WHITE);
			mPaint.setTextSize(mLeftCharSize);
			
			int mTopSpec = mWeekBarHeight;
			for( int i=0; i<16; i++ ){
				Time[] time = CourseUtils.getClassIntervalByClassTime(i, i);
				String text1 = CHINESENUMBERCHAR[i];
				String text2 = "" + timeFormater(time[0]) + "-" + timeFormater(time[1]);
				int y = mTopSpec + mRowHeight*i+mCourseInfoCharSize;
				canvas.drawText(text1, 3, y+2, mPaint);
				canvas.drawText(text2, 3, y+mCourseInfoCharSize+5, mPaint);
			}
		}

		private String timeFormater( Time time ){
			return time.hour + ":" + String.format("%02d", time.minute);
		}

		private void drawWeekText( Canvas canvas ){
			mPaint.setTextSize(mWeekCharSize);
			mPaint.setColor(Color.WHITE);
			
			int leftSpec = mLeftBarWidth;
			
			for( int i=0; i<6; i++ ){
				canvas.drawText("星期" + CHINESENUMBERCHAR[i], leftSpec + mColumnWidth*i+3, mWeekCharSize+3, mPaint);
			}
		}
	}
	
	private class SubCourseView extends TextView{

		Course aCourse;
		public SubCourseView(Context context, Course course) {
			super(context);
			aCourse = course;
			setGravity(Gravity.CENTER);
			setBackgroundColor( 0xff4972cf );
			setTextSize(mCourseInfoCharSize);
			setTextColor(Color.BLACK);
			setText(aCourse.getCourseName() + "\n" + aCourse.getPlace());
		}
		
		@Override
		protected void onMeasure( int width, int height ){
			width = mColumnWidth;
			height = mRowHeight*aCourse.getClassCount();
			setMeasuredDimension(width, height);
		}
		
		public Course getCourse(){
			return aCourse;
		}
		
		@Override
		public void onDraw(Canvas canvas){
			super.onDraw(canvas);
			
		}
		
	}
}
